<!--

    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.

-->
<html>
<body>

Representation of Java classpaths, and the ability to find the classpath needed
for a particular purpose.

<p>Using the Classpath API you can look up the classpath used for a certain
purpose (such as compilation or running) on a certain file.</p>

<h2>
Contents
</h2>
<ul>
<li><a href="#overview">Overview</a>
<li><a href="#obtaining-classpath">How to obtain a ClassPath</a>
<li><a href="#roots-definition">Roots and the definition of the ClassPath</a>
<li><a href="#resources-files">Resource names and FileObjects</a>
<li><a href="#change-listen">Listening to classpath changes</a>
<li><a href="#filesystem-replacements">Replacements of FileSystems API methods</a>
</ul>

<h2>ClassPath API</h2>

<h3 id="overview">Overview</h3>

<p>At runtime, there's often only a single Classpath available through the application
classloader. The application is often developed against a set of APIs, with some
implementation provided at runtime. For example, if using JDBC, the application is
compiled against JDBC API only and the proper driver is added to the runtime classpath.
To avoid unintentional dependencies on implementation, the implementation classes
are often stripped off the compilation path. Another case is debugging with special
implementation libraries that - for example - perform special sanity checks or
logging. Again, the classpath is different from those used during build or 
application's execution.
<p>
Since the IDE supports the whole development cycle, it needs to provide <i>different
classpaths</i> for each of the purposes mentioned, and maybe for others, too. 
See {@link java.lang.ClassLoader}.

<h3 id="obtaining-classpath">Obtaining ClassPath</h3>

<p>The ClassPath API uses concept similar to <a href="@org-openide-util@/org/openide/util/doc-files/api.html#service-lookup">
Services</a> to categorize ClassPaths. Instead of the class, a string token
is used to name a service type. 
Various types such as {@link org.netbeans.api.java.classpath.ClassPath#COMPILE}
are defined.
<p>
In addition to different services, the API also permits different files to have
different classpaths configured for them. In addition to the desired
classpath type, clients are required to pass in a FileObject of the user file, which
is the "starting point" for the classpath search.
<p>
To obtain a ClassPath instance for the given FileObject and intended usage, you 
want to call {@link org.netbeans.api.java.classpath.ClassPath#getClassPath}
as in this example for getting compilation classpath:

<div class="nonnormative">
<pre>
<span class="type"><a href="@org-openide-filesystems@/org/openide/filesystems/FileObject.html">FileObject</a></span> <span class="variable-name">f</span> = <span class="function-name">getSomeJavaSourceFile</span>();
<span class="type"><a href="ClassPath.html">ClassPath</a></span> <span class="variable-name">cp</span> = ClassPath.<a href="ClassPath.html#getClassPath(org.openide.filesystems.FileObject,java.lang.String)"><span class="function-name">getClassPath</span></a>(f, ClassPath.<a href="ClassPath.html#COMPILE"><span class="constant">COMPILE</span></a>);
<span class="predefined">System.err.println</span>(cp);
</pre>
</div>

<h3 id="roots-definition">Roots and definition of the classpath</h3>

<p>The <i>classpath</i> is formed from one or more roots. <i>JRE</i> itself supports
at least two types of of them: a root can be either a directory or <i>an archive</i>
(<code>.jar</code> or <code>.zip</code> file). The definition of the classpath cannot
maintain integrity with the root folders or archives. If one of them is renamed,
deleted or otherwise changed, its entry in the classpath becomes <i>invalid</i> and
does not contribute to the contents of the classpath. Note that the entry cannot
be removed automagically, since an archive can disappear (for example) because of
the compilation products in a subproject. 
<b>Note</b> that regardless of where the classpath entry is an archive, or a folder,
the <i>root</i> returned for it will be <b>always a folder</b>. For archive files,
it will be the root folder of the <i>archive filesystem</i>.
<p>
For most purposes, working with only those roots which are valid, is sufficient enough.
The error condition can be detected when a required resource is missing, for example.
For some tasks checking for validity of individual classpath entries may be appropriate,
the build process being a notable example. For others, like searches or parsing,
such behaviour is hihgly undesirable.
<p>
If you don't need to notify the user that the <i>definition</i> of classpath is 
not correct (there are other ways or another, more appropriate, time to notify the user),
you can use
{@link org.netbeans.api.java.classpath.ClassPath#getRoots}
to find out what folders are the roots:

<div class="nonnormative">
<pre>
<a href="ClassPath.html"><span class="type">ClassPath</span></a> <span class="variable-name">cp</span> = ClassPath.<a href="ClassPath.html#getClassPath(org.openide.filesystems.FileObject,java.lang.String)"><span class="function-name">getClassPath</span></a>(myClassFile, ClassPath.<a href="ClassPath.html#EXECUTE"><span class="constant">EXECUTE</span></a>);
<a href="@org-openide-filesystems@/org/openide/filesystems/FileObject.html">FileObject</a>[] <span class="variable-name">roots</span> = cp.<a href="ClassPath.html#getRoots()"><span class="function-name">getRoots</span></a>();
<span class="keyword">for</span> (<span class="keyword">int</span> <span class="variable-name">i</span> = <span class="constant">0</span>; i &lt; roots.length; i++) {
    System.err.print(roots[i] + <span class="constant">":"</span>);
}
</pre>
</div>

<p>Clients who need to find out whether the environment is set up well, may work
more thoroughly using
{@link org.netbeans.api.java.classpath.ClassPath.Entry} objects:

<div class="nonnormative">
<pre>
<a href="ClassPath.html"><span class="type">ClassPath</span></a> <span class="variable-name">cp</span> = ClassPath.<a href="ClassPath.html#getClassPath(org.openide.filesystems.FileObject,java.lang.String)"><span class="function-name">getClassPath</span></a>(myClassObject, ClassPath.<a href="ClassPath.html#EXECUTE"><span class="constant">EXECUTE</span></a>);
<a href="@JDK@@JDKMODULE_JAVA_BASE@/java/util/List.html"><span class="type">List</span></a> <span class="variable-name">entries</span> = cp.<a href="ClassPath.html#entries()"><span class="function-name">entries</span></a>();
<span class="keyword">for</span> (Iterator <span class="variable-name">it</span> = entries.iterator(); it.hasNext(); ) {
    <a href="ClassPath.Entry.html"><span class="type">ClassPath.Entry</span></a> <span class="variable-name">en</span> = (ClassPath.Entry)it.next();
    <span class="keyword">if</span> (!en.<a href="ClassPath.Entry.html#isValid()"><span class="function-name">isValid()</span></a>) {
        <span class="type">IOException</span> <span class="variable-name">x</span> = en.<a href="ClassPath.Entry.html#getError()"><span class="function-name">getError</span></a>();
        <span class="comment">// Report the error somehow, perhaps using <a href="@org-openide-util-ui@/org/openide/ErrorManager.html"><code>org.openide.ErrorManager</code></a></span>.
    } <span class="keyword">else</span> {
        <a href="@org-openide-filesystems@/org/openide/filesystems/FileObject.html"><span class="type">FileObject</span></a> <span class="variable-name">root</span> = en.<a href="ClassPath.Entry.html#getRoot()"><span class="function-name">getRoot</span></a>();
        <span class="comment">// Do something with the root folder</span>
    }
}
</pre>
</div>

<h3 id="resources-files">Resource Names and FileObjects</h3>

<p>The <b>resource name</b> is essentially a 
file name, relative to the root of classpath. If the classpath has more roots (it's
a <i>classpath forest</i> rather than a single tree),
they are combined and merged together to give one "logical" tree. The merge operation
has one subtle property, <b>resource hiding</b>: when there are several resources of
the same name in the classpath forest, the order of the roots that define the
classpath matters and the only the first resource encountered (in that order)
is <i>visible</i> for the ClassLoader.
<p>
Often a FileObject is viewed as a resource to the application being developed.
It's the most used view for Java sources, as they are required to appear in
an appropriate folder in the source tree, but for other objects as well. For example,
the IDE support for images need to record the <i>resource name</i> of the image
so that it can be loaded at runtime using
{@link java.lang.ClassLoader#getResourceAsStream}.
If you need to obtain a name for FileObject,
which is <i>relative to the classpath</i>, you first have to think about <b>for which
service do you need it</b>. For resource bundles, for example, it will be mostly the <i>execution</i>
service. Then you will get the appropriate {@link org.netbeans.api.java.classpath.ClassPath}
instance and ask it to compute the name for you:

<div class="nonnormative">
<pre>
<a href="@org-openide-filesystems@/org/openide/filesystems/FileObject.html"><span class="type">FileObject</span></a> <span class="variable-name">f</span> = bundleDataObject.<a href="@org-openide-loaders@/org/openide/loaders/DataObject.html#getPrimaryFile()"><span class="function-name">getPrimaryFile</span></a>();
<a href="ClassPath.html"><span class="type">ClassPath</span></a> <span class="variable-name">cp</span> = ClassPath.<a href="ClassPath.html#getClassPath(org.openide.filesystems.FileObject,java.lang.String)"><span class="function-name">getClassPath</span></a>(theSourceDataObject.getPrimaryFile(), ClassPath.<a href="ClassPath.html#EXECUTE"><span class="constant">EXECUTE</span></a>);
String <span class="variable-name">bundleResource</span> = cp.<a href="ClassPath.html#getResourceName(org.openide.filesystems.FileObject)"><span class="function-name">getResourceName</span></a>(f);
</pre>
</div>

<p>To check whether a resource (<code>FileObject</code>) is visible or not to the service,
you may want to call {@link org.netbeans.api.java.classpath.ClassPath#isResourceVisible}.
You may want to do just the opposite, to get a <code>FileObject</code> for a <i>resource name</i>
you have. Since there may be more resources of that name, the API supports
{@link org.netbeans.api.java.classpath.ClassPath#findResource} to get the visible one,
or {@link org.netbeans.api.java.classpath.ClassPath#findAllResources} to get
a collection of all resources matching the name.

<h3 id="change-listen">Listening to changes in ClassPath</h3>

<p>The <code>ClassPath</code> interface supports listening to either <i>root folder set changes</i>
or <i>entry set changes</i>. Changes in root folder set can be observed by 
property change listeners, they are reported as additions,
deletions or changes to the order of roots. For example, to watch out for new compile-time
dependencies, you may do:

<div class="nonnormative">
<pre>
<a href="@org-openide-filesystems@/org/openide/filesystems/FileObject.html"><span class="type">FileObject</span></a> <span class="variable-name">f</span>; <span class="comment">// some interesting FileObject.</span>
<a href="ClassPath.html"><span class="type">ClassPath</span></a> <span class="variable-name">cp</span> = ClassPath.<a href="ClassPath.html#getClassPath(org.openide.filesystems.FileObject,java.lang.String)"><span class="function-name">getClassPath</span></a>(f, ClassPath.<a href="ClassPath.html#COMPILE"><span class="constant">COMPILE</span></a>);
cp.<a href="ClassPath.html#addPropertyChangeListener(java.beans.PropertyChangeListener)"><span class="function-name">addPropertyChangeListener</span></a>(new <a href="@JDK@@JDKMODULE_JAVA_DESKTOP@/java/beans/PropertyChangeListener.html"><span class="type">PropertyChangeListener</span></a>() {
    <span class="keyword">public void</span> <span class="function-name">propertyChange</span>(<a href="@JDK@@JDKMODULE_JAVA_DESKTOP@/java/beans/PropertyChangeEvent.html"><span class="type">PropertyChangeEvent</span></a> <span class="variable-name">evt</span>) {
        <span class="keyword">if</span> (ClassPath.<a href="ClassPath.html#PROP_ROOTS"><span class="constant">PROP_ROOTS</span></a>.equals(evt.<a href="@JDK@@JDKMODULE_JAVA_DESKTOP@/java/beans/PropertyChangeEvent.html#getPropertyName()"><span class="function-name">getPropertyName()</span></a>)) {
            <span class="comment">// Update your stuff, because classpath roots have changed.</span>
        }
    }
});
</pre>
</div>

<p>You may also listen <code><i>entries</i></code> property. Note that the <i>root 
folder set</i> may change even though the <i>entry set</i> did not change,
as a result of some entry becoming (in)valid.

<h3 id="filesystem-replacements">Replacements of FileSystems API methods</h3>

<p>The <b>now-deprecated</b> way how to 
obtain a resource name for a file object was to call 
{@link org.openide.filesystems.FileObject#getPackageNameExt}.
But the <code>getPackageNameExt()</code> method gives a name
of the file within the <code>FileSystem</code> rather than its relative path
from some classpath root point. Therefore it is no longer recommended to use the
{@link org.openide.filesystems Filesystems API}
to obtain classpaths.
<p>
As the {@link org.openide.filesystems.Repository}
searches through FileSystems without regard to the intended usage for individual services,
{@link org.openide.filesystems.Repository#findResource} and
{@link org.openide.filesystems.Repository#findAllResources}
should not be used at all.
{@link org.netbeans.api.java.classpath.GlobalPathRegistry} may be used to find certain kinds of files that are
available among open projects in the GUI, but this should be used only as a last
resort if there is no other possible source of information about where a file
might be located according to specific classpaths.
The usage of the class
{@code org.openide.filesystems.FileSystemCapability}
is <b>deprecated</b>;
you should use methods of
{@link org.netbeans.api.java.classpath.ClassPath}
instead of that.

<table border="1">
    <caption>Summary of deprecated methods and their replacements</caption>
<tr>
<th style="align:center">Old method</th><th style="align:center">New method</th>
</tr>
<tbody>
<tr>
<td>{@link org.openide.filesystems.FileObject#getPackageName} and {@link org.openide.filesystems.FileObject#getPackageName}</td>
<td>{@link org.netbeans.api.java.classpath.ClassPath#getResourceName}</td>
</tr>
<tr>
<td>{@code org.openide.filesystems.FileSystemCapability#findResource}</td>
<td>{@link org.netbeans.api.java.classpath.ClassPath#findResource}</td>
</tr>
<tr>
<td>{@code org.openide.filesystems.FileSystemCapability#findAllResources}</td>
<td>{@link org.netbeans.api.java.classpath.ClassPath#findAllResources}</td>
</tr>
<tr>
<td>{@code org.openide.filesystems.FileSystemCapability#fileSystems}</td>
<td>{@link org.netbeans.api.java.classpath.ClassPath#getRoots}</td>
</tr>
<tr>
<td>{@link org.openide.filesystems.Repository#findResource}</td>
<td>{@link org.netbeans.api.java.classpath.GlobalPathRegistry#findResource}</td>
</tr><tr>
<td>{@link org.openide.filesystems.Repository#findAllResources}</td>
<td>{@link org.netbeans.api.java.classpath.GlobalPathRegistry#getSourceRoots}</td>
</tr>
</tbody>
</table>
  
</body>
</html>
